---
title: "__getattr__ and getattribute"
date: 2018-11-10
categories:
- python
tags:
---

<div id="content">
<blockquote>
<p>
<a href="https://stackoverflow.com/questions/4295678/understanding-the-difference-between-getattr-and-getattribute">https://stackoverflow.com/questions/4295678/understanding-the-difference-between-getattr-and-getattribute</a>
</p>
</blockquote>
<p>
获取对象的属性：instance.attribute，
通过属性的名字获取：getattr(instance, attribute_name)
</p>
<p>
重写这两个方法能自定义获取属性时的行为，不同之处在于获取属性的时候：
</p>
<ul class="org-ul">
<li>__getattr__方法仅仅在属性不存在于对象中时被调用。</li>
<li>面__getattribute__总是被调用，无论发生是否存在。使用它很容易造成无限递归调用。</li>
</ul>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2018-11-10</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
